package cn.smbms.controller;

import cn.smbms.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Controller
@RequestMapping("/test")
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)//解决spring mvc单例模式的数据安全问题
public class TestController {

    private List<User> userList = new ArrayList<User>();

    public TestController(){
        try {//初始化用户数据
            userList.add(new User(1, "test001","测试用户001", "1111111", 1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1986-12-10"),
                    "13566669998", "北京市朝阳区北苑", 1, 1, new Date(), 1,
                    new Date()));
            userList.add(new User(2, "zhaoyan","赵燕", "2222222", 1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1984-11-10"),
                    "18678786545", "北京市海淀区成府路", 1, 1,new Date(), 1,
                    new Date()));
            userList.add(new User(3, "test003","测试用户003", "3333333", 1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1980-11-11"),
                    "13121334531", "北京市通州北苑", 1,1, new Date(), 1, new Date()));
            userList.add(new User(4, "wanglin","王林", "4444444", 1,
                    new SimpleDateFormat("yyyy-MM-dd").parse("1989-09-10"),
                    "18965652364", "北京市学院路",1, 1, new Date(),1, new Date()));
            userList.add(new User(5, "zhaojing","赵静", "5555555", 1, new
                    SimpleDateFormat("yyyy-MM-dd").parse("1981-08-01"),
                    "13054784445","北京市广安门",1, 1,new Date(), 1, new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    private List<User> queryList = new ArrayList<>();//2个
    private Logger logger = Logger.getLogger(TestController.class.getName());
    @RequestMapping("/index")
    public String index(Model model,
                        @RequestParam(value = "userName",required = false) String userName){
        List<User> queryList = new ArrayList<User>();
        //数字或者英文
        logger.info("userName====>"+userName);
        if (userName == null || userName.equals("")){
            model.addAttribute("userList",userList);
        }else {
//            userList.forEach(user -> {
//                if (user.getUserName().indexOf(userName) != -1){
//                    queryList.add(user);
//                }
//            });
            for (User user : userList) {
                if (user.getUserName().indexOf(userName) != -1){
                    queryList.add(user);//4
                }
            }
            model.addAttribute("userList",queryList);
        }
        return "user/userlist";
    }

    @RequestMapping("/index2")
    public String index2(){
        throw new RuntimeException("测试类异常!!");
    }
}
